package com.fowo.api.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.R;
import com.fowo.api.sys.entity.SysApplication;
import com.fowo.api.sys.model.*;
import com.fowo.api.sys.service.SysApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Tag(name = "系统应用")
@RequestMapping("/api/sys/application")
@RestController
public class SysApplicationController {

    @Resource
    private SysApplicationService applicationService;

    @Operation(summary = "全部（不分页）")
    @GetMapping
    public R<List<SysApplicationItemVo>> search(SysApplicationSearch search) {
        return R.data(applicationService.search(search));
    }

    @Operation(summary = "查询分页")
    @GetMapping("page")
    public R<Page<SysApplicationItemVo>> page(SysApplicationSearch search) {
        return R.data(applicationService.searchPage(search));
    }

    @Operation(summary = "获取指定 id 的记录")
    @GetMapping("{id}")
    public R<SysApplication> getById(@PathVariable Long id) {
        return R.data(applicationService.getById(id));
    }

    @Operation(summary = "创建")
    @PostMapping
    public R<SysApplication> create(@Validated @RequestBody CreateSysApplication model) {
        return R.data(() -> applicationService.create(model));
    }

    @Operation(summary = "删除")
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable Long id) {
        return R.run(() -> applicationService.removeById(id));
    }
}
